package com.lindroy.todomvp.addtask

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.lindroy.todomvp.R
import com.lindroy.todomvp.data.source.TasksDataSource
import com.lindroy.todomvp.data.source.TasksRepository
import com.lindroy.todomvp.util.replaceFragmentInActivity
import com.lindroy.todomvp.util.setupActionBar
import kotlinx.android.synthetic.main.activity_add_edit_task.*

/**
 * @author Lin
 * @date 2021/6/25
 * @function 添加任务
 * @Description
 */
class AddEditTaskActivity : AppCompatActivity() {

    private lateinit var addEditTaskPresenter: AddEditTaskPresenter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_edit_task)
        val taskId = intent.getStringExtra(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID)
        setupActionBar(toolbar){
            setDisplayHomeAsUpEnabled(true)
            setDisplayShowHomeEnabled(true)
            setTitle(if (taskId == null) R.string.add_task else R.string.edit_task)
        }
        val addEditTaskFragment =
            supportFragmentManager.findFragmentById(R.id.contentFrame) as AddEditTaskFragment?
                ?: AddEditTaskFragment.newInstance(taskId).also {
                    replaceFragmentInActivity(it, R.id.contentFrame)
                }

        addEditTaskPresenter = AddEditTaskPresenter(taskId,TasksRepository(),addEditTaskFragment,true)
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    companion object {
        const val SHOULD_LOAD_DATA_FROM_REPO_KEY = "SHOULD_LOAD_DATA_FROM_REPO_KEY"
        const val REQUEST_ADD_TASK = 1
    }

}